package com.android.nurse;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Html;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Explain screen
 * 
 * @author Thanhnd
 * 
 */
public class ExplainedScreen extends CommonActivity {

	// go to ranking
	private Button btnGoRanking;

	// image of nurse type
	private ImageView imageType;

	// nickname
	private TextView lblNickName;

	// tired type
	private TextView lblTypeTired;
	
	// gender
	private TextView lblGender;

	// tired description
	private TextView lblTypeDes;

	// tired level
	private TextView lblLevelTired;
	private TextView lblTiredDes;
	private int tiredPoint;
	private int idNurse;
	private String userName;
	private NurseType nurse;
	private int width;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Display display = ((WindowManager) this
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		width = display.getWidth();
		Log.d("chieu rong man hinh", ""+width);
		if(width > 500){
			setContentView(R.layout.explained_layout_big);
		} else {
		setContentView(R.layout.explained_layout);
		}
		this.getDisplayWidth();
		
		imageType = (ImageView) findViewById(R.id.imgTiredType);
		lblNickName = (TextView) findViewById(R.id.lblNickName);
		lblTypeTired = (TextView) findViewById(R.id.lblTypeName);
		lblTypeDes = (TextView) findViewById(R.id.lblTypeDes);
		lblLevelTired = (TextView) findViewById(R.id.lblLevelTired);
		lblTiredDes = (TextView) findViewById(R.id.lblTiredDes);
		lblGender = (TextView) findViewById(R.id.lblGender);
		btnGoRanking = (Button) findViewById(R.id.btnGoRanking);
		myThread.start();
		getData();
		setContentExplain(idNurse, userName, tiredPoint);
		// listen go Ranking
		
		
	}
	
	@Override
	public void onBackPressed() {
	   return;
	}
	
	public void getDisplayWidth() {
		width = 0;
		Display display = getWindowManager().getDefaultDisplay(); 
		width = display.getWidth();
		if(width == 0){
			DisplayMetrics displaymetrics = new DisplayMetrics();
	        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
	        width = displaymetrics.widthPixels;
		}
	}

	/**
	 * Analysis file json
	 * 
	 * @param json
	 *            : content of Json file
	 * @throws Exception
	 */
	public void setContentExplain(int idType, String nickName, int levelTired) {
		String strTypeName = "";
		String strTypeDes = "";
		String strTiredDes = "";
		nurse = getNurseType(idType);
		strTypeName = nurse.getName();
		strTypeDes = nurse.getDescription();

		TiredDescription tired = getTiredDescription(getIdTired(tiredPoint));
		strTiredDes = tired.getDescription();

		// get information for show
		setContentExplained(nickName, strTypeName, strTypeDes, levelTired,
				strTiredDes);
	}

	/**
	 * Get information for show on component
	 * 
	 * @param imgType
	 * @param nickName
	 * @param typeTired
	 * @param typeDes
	 * @param levelTired
	 * @param tiredDes
	 */
	private void setContentExplained(String nickName, String typeTired,
			String typeDes, int levelTired, String tiredDes) {
		
		this.imageType.setImageResource(getResources().getIdentifier(
				nurse.getImageS(), "drawable", getPackageName()));
		
		this.lblNickName.setTextSize(TypedValue.COMPLEX_UNIT_PT,((((float)width)/480) * 9));
		
		Log.d("image","text width: " +((((float)width)/480) * 12));
		this.lblGender.setTextSize(TypedValue.COMPLEX_UNIT_PT,((((float)width)/480) * 7));
		this.lblNickName.setText(nickName);
		this.lblTypeTired.setText(typeTired);
		this.lblTypeDes.setText(typeDes);
		this.lblLevelTired.setText(Html.fromHtml("<font color='#fd6396'>お疲れ度 " + levelTired + "%</font> のあなたは…"));
		this.lblTiredDes.setText(tiredDes);
	}

	public void getData() {
		SharedPreferences settings = getSharedPreferences(
				QuestionsScreen.PREFS_NAME, 0);
		userName = settings.getString(LogInScreen.userName, "no exits!");
		idNurse = settings.getInt("ID_NURSE", 10);
		tiredPoint = settings.getInt("TIRED_POINT", 1000);
	}

	/**
	 * return tired id by point.
	 * 
	 * @param point
	 * @return tired id
	 */
	public int getIdTired(int point) {
		if (point <= 50) {
			return Constant.LOW_TIRED_ID;
		} else if (point > 50 && point <= 75) {
			return Constant.MIDDLE_TIRED_ID;
		} else {
			return Constant.HIGHT_TIRED_ID;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.getDisplayWidth();
	}
private Thread myThread = new Thread(new Runnable() {
		
		@Override
		public void run() {
			btnGoRanking.setClickable(false);
			synchronized (this) {
				try {
					wait(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			btnGoRanking.setClickable(true);
			btnGoRanking.setOnTouchListener(new OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					switch (event.getAction() & MotionEvent.ACTION_MASK) {

					case MotionEvent.ACTION_DOWN:
						if(width > 500){
							btnGoRanking.setBackgroundResource(R.drawable.btn_ranking_big_2);
						} else {
						btnGoRanking.setBackgroundResource(R.drawable.btn_ranking_2);
						}
						break;
					case MotionEvent.ACTION_UP:
						if(width > 500){
							btnGoRanking.setBackgroundResource(R.drawable.btn_ranking_big);
						} else {
						btnGoRanking.setBackgroundResource(R.drawable.btn_ranking);
						}
						Intent i = new Intent(getApplicationContext(),
								RankingActivity.class);
						customeActivity(i);
						break;
					}
					return true;
				}
			});
		}
	});
}
